package com.tl.khadmin.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.tl.khadmin.bean.Staff;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.bean.SupplierBankAccount;
import com.tl.khadmin.mapper.SupplierBankAccountMapper;
import com.tl.khadmin.mapper.SupplierMapper; 
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.SupplierIService; 
import com.tl.khadmin.util.CommonUtil;
import com.tl.khadmin.util.Constant;
import com.tl.khadmin.util.PutParamUtil; 

@Service
public class SupplierServiceImpl implements SupplierIService{
	
	@Resource
	private SupplierMapper supplierMapper;
	@Resource
	private SupplierBankAccountMapper supplierBankAccountMapper;
	
	/**
	 * 分页查供应商银行账号
	 */
	@Override
	public Page<SupplierBankAccount> showBankaccountByPage(HttpSession session, 
			Integer pageNum, Integer pageSize, Map<String, Object> params) {
		Page<SupplierBankAccount> page=new Page<SupplierBankAccount>(pageNum, pageSize);
        params.put("page", page);   
        Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
        PutParamUtil.setParamIsInterior(supplier, params);	//设置查询供应商数据范围 
        List<SupplierBankAccount> list= supplierBankAccountMapper.showByPage(params);  
		page.setResult(list);
		return page;
	}
	
	public List<Supplier> listSupplier(Map<String, Object> param){
		param.put("isDelete", 0);
		return supplierMapper.listSupplier(param); 
	} 
	
	@Override
	public String updateSupplierStatus(Supplier supplier) {
		Supplier su = new Supplier(); 
		su.setStatus(supplier.getStatus()); 
		su.setId(supplier.getId());  
		int result = supplierMapper.updateByPrimaryKeySelective(su);
		return CommonUtil.returnMapResultByRows(result, "提交成功", "提交失败，请重试");
	} 
	
	@Override
	public String auditSupplier(HttpSession session, Supplier supplier) {
		Supplier su = new Supplier();
		Staff staff = (Staff) session.getAttribute(Constant.USER); 
		su.setAuditorId(staff.getId()); 
		su.setAuditOpinion(supplier.getAuditOpinion()); 
		su.setAuditTime(new Date());
		su.setAuditStatus(supplier.getAuditStatus());
		su.setId(supplier.getId());  
		su.setPaymentCycle(supplier.getPaymentCycle());
		int result = supplierMapper.updateByPrimaryKeySelective(su);
		return CommonUtil.returnMapResultByRows(result, "提交成功", "提交失败，请重试");
	}
	
	@Override
	public Page<Supplier> showSupplierByPage(HttpSession session, Integer pageNum, Integer pageSize, Map<String, Object> params) {
		Page<Supplier> page=new Page<Supplier>(pageNum, pageSize);
        params.put("page", page);   
        params.put("isDelete", 0);
        Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
        PutParamUtil.setParamIsInterior(supplier, params);	//设置查询供应商数据范围 
        List<Supplier> list= supplierMapper.showSupplierByPage(params);  
		page.setResult(list);
		return page;
	} 
	
	@Override
	public String deleteByIds(String ids) {
		String[] idsArr = ids.substring(0,ids.length()-1).split(",");
		try{
			int result = supplierMapper.updateDeleteByIds(idsArr);
		}catch(Exception ex) {
			return CommonUtil.returnMapResultByRows(0, "删除成功","删除失败，请重试");
		} 
		return CommonUtil.returnMapResultByRows(1, "删除成功","删除失败，请重试");
	}

	@Override
	public Map<String, Object> querySupplierById(Long id) {
		Map<String, Object> result = new HashMap<>();
		Supplier supplier = supplierMapper.selectByPrimaryKey(id); 
		result.put("supplier", supplier); 
		return result;
	}

	@Override
	public String deleteSupplier(Supplier supplier) {
		if(supplier.getId() == null){
			return CommonUtil.returnMapResultByRows(0, "参数错误","删除失败，请重试");
		} 
		Supplier temp = new Supplier();
		temp.setId(supplier.getId());
		temp.setIsDelete(1);
		int result = supplierMapper.updateByPrimaryKeySelective(temp);
		return CommonUtil.returnMapResultByRows(result, "提交成功", "提交失败，请重试");
	}

	@Override
	public String updateSupplier(HttpSession session, Supplier supplier) {
		Supplier supp = new Supplier();
		supp.setId(supplier.getId());
		supp.setPaymentCycle(supplier.getPaymentCycle()); 
		supp.setAddress(supplier.getAddress()); 
		return CommonUtil.returnMapResultByRows(supplierMapper.updateByPrimaryKeySelective(supp), "提交成功", "提交失败，请重试");
	}

	@Override
	public String auditSupplierBankAccount(HttpSession session, SupplierBankAccount bankAccount) {
		SupplierBankAccount temp = new SupplierBankAccount();
		temp.setId(bankAccount.getId());
		temp.setStatus(bankAccount.getStatus());
		int result = supplierBankAccountMapper.updateByPrimaryKeySelective(temp);
		return CommonUtil.returnMapResultByRows(result, "提交成功", "提交失败，请重试");
	}
}
